/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.service.impl;

import com.mavenrentcars.dao.ReservationDao;
import com.mavenrentcars.exception.ViewException;
import com.mavenrentcars.form.OrderForm;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Place;
import com.mavenrentcars.model.Reservation;
import com.mavenrentcars.model.User;
import com.mavenrentcars.service.AccountService;
import com.mavenrentcars.service.CarService;
import com.mavenrentcars.service.PlaceService;
import com.mavenrentcars.service.ReservationService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;

/**
 *
 * @author serg
 */
@Service("reservationService")
public class ReservationServiceImpl implements ReservationService {

    @Resource(name = "reservationDao")
    ReservationDao reservationDao;
    @Resource(name = "accountService")
    AccountService accountService;
    @Resource(name = "placeService")
    PlaceService placeService;
    @Resource(name = "carService")
    CarService carService;

    @Override
    public Reservation save(Reservation model) {
        return reservationDao.save(model);
    }

    @Override
    public Reservation get(Integer id) {
        return reservationDao.get(id);
    }

    @Override
    public void remove(Integer id) {
        reservationDao.remove(id);
    }

    @Override
    public List<Reservation> getAll() {
        return reservationDao.getAll();
    }

    @Override
    public Reservation add(OrderForm form) throws ViewException {
        try {
            Car car = carService.get(Integer.parseInt(form.getCar()));
            User user = accountService.getLoggedInAccount();
            Place place = placeService.get(Integer.parseInt(form.getFinishPlace()));

            Reservation item = new Reservation();
            item.setUser(user);
            item.setCar(car);
            item.setFinishPlace(place);

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            item.setBeginDate(df.parse(form.getOrderFrom()));
            item.setEndDate(df.parse(form.getOrderTo()));
            item.setStatus("waited");

            Reservation res = reservationDao.save(item);
            return res;
        } catch (ParseException ex) {
            throw new ViewException(ex.getMessage());
        }
    }

    @Override
    public List<Reservation> getAll(User user) {
        return reservationDao.getAll(user);
    }

    @Override
    public List<Reservation> getAll(Car car) {

        //DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
        Date start_time = cal.getTime();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 2, 1);
        Date end_time = cal.getTime();

        return reservationDao.getAll(car, start_time, end_time);
    }

    @Override
    public Set<String> getByCarDays(Car car) {
        Set<String> res = new HashSet<String>();
        List<Reservation> list = getAll(car);
        SimpleDateFormat df = new SimpleDateFormat("M-d-yyyy");
        Calendar start_cal = Calendar.getInstance();
        Calendar end_cal = Calendar.getInstance();

        for (Reservation ritem : list) {
            Date start = ritem.getBeginDate();
            Date end = ritem.getEndDate();

            start_cal.setTime(start);
            long diff_start = start_cal.getTimeInMillis();

            end_cal.setTime(end);
            long diff_end = end_cal.getTimeInMillis();

            res.add(df.format(start_cal.getTime()));
            long diff = diff_end - diff_start;
            while (diff > 0) {

                start_cal.add(Calendar.DATE, 1);
                res.add(df.format(start_cal.getTime()));

                diff_start = start_cal.getTimeInMillis();
                diff = diff_end - diff_start;
            }
        }

        return res;
    }

    @Override
    public String getJasonByCarDays(Car car) {
        JSONObject jsonObject = new JSONObject();
        Set<String> list = this.getByCarDays(car);
        int i = 0;
        for (String item : list) {
            jsonObject.put(i, item);
            i++;
        }
        return jsonObject.toString();
    }
}
